﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Collections;
using CYP.IISMonitor;
using CYP.IISMonitor.Utility.Mails;
using CYP.IISMonitor.Utility.SMS;
using CYP.IISMonitor.Utility.Files;
using CYP.IISMonitor.Utility.XML;
using System.Data;
using System.Xml;
using System.IO;
using CYP.IISMonitor.Utility;

namespace CYP.IISMonitor
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new IISMonitorService() 
            };
            ServiceBase.Run(ServicesToRun);

            #region TEST
            /*
            try
            {
                System.Timers.Timer timer = new System.Timers.Timer();
                timer.Interval = Convert.ToInt64(ConfigHelper.GetConfig("SvcInterval"));
                timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
                timer.Start();

                //Invoke();

                //Console.WriteLine("Service is started!");
                //Console.ReadLine();
            }
            catch (System.Net.WebException ex)
            {
                Console.WriteLine("Service is Exception!");
                Console.ReadLine();
            }
             */
            #endregion
        }

        #region TEST
        /*
        #region MyRegion

        static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Invoke();
        }


        private static XmlDocument doc = new XmlDocument();
        //网站信息文件目录全路径
        private static string filePath = "Websites.xml";

        /// <summary>
        /// 调用方法
        /// </summary>
        private static void Invoke()
        {
            try
            {
                //获取所有站点信息
                doc.Load(filePath);
                XmlNodeList listWebsite = doc.SelectNodes("//Websites//Website");
                foreach (XmlNode item in listWebsite)
                {
                    //模拟访问页面，检测IIS是否可用。
                    string strIISAddreess = item.Attributes["iisAddress"].Value;
                    string strDataAddreess = item.Attributes["dataAddress"].Value;
                    bool boIIS = TestIISAdrress(strIISAddreess);
                    if (!boIIS)
                    {
                        RecordError(item, AddressType.IIS);
                    }
                    else
                    {
                        bool boData = TestDataAdrress(strDataAddreess);
                        if (!boData)
                        {
                            RecordError(item, AddressType.DATA);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //程序异常，记录异常日志
            }
        }

        private enum AddressType
        {
            IIS,
            DATA
        }

        private static void RecordError(XmlNode item, AddressType type)
        {
            //发送短信和邮件
            if (Convert.ToBoolean(item.Attributes["notification"].Value))
            {
                int i = UpdateTimesByWebsiteName(item, String.Empty);
                if (i <= Convert.ToInt32(item.Attributes["notificationMax"].Value))
                {
                    string msg = String.Empty;
                    switch (type)
                    {
                        case AddressType.IIS:
                            msg = String.Format("站点[{0}]IIS访问出错。", item.Attributes["name"].Value);
                            break;
                        case AddressType.DATA:
                            msg = String.Format("站点[{0}]数据库访问出错。", item.Attributes["name"].Value);
                            break;
                        default:
                            msg = String.Format("站点[{0}]IIS访问出错。", item.Attributes["name"].Value);
                            break;
                    }

                    if (Convert.ToBoolean(item.Attributes["smsEnable"].Value))
                    {
                        SMSManager.Send(msg);
                    }
                    MailManager.Send(msg);
                }
            }
            //记录日志文件
            //_trace.Log("test");
        }

        /// <summary>
        /// 模拟访问页面，检测IIS是否可用。
        /// </summary>
        /// <param name="adrress"></param>
        /// <returns></returns>
        private static bool TestIISAdrress(string adrress)
        {
            bool bo = false;
            try
            {
                //实例化模拟客户端请求对象
                System.Net.WebRequest webRequest = System.Net.WebRequest.Create(adrress);
                //得到请求的响应对象
                System.Net.WebResponse webResponse = webRequest.GetResponse();
                bo = true;
            }
            catch (Exception ex)
            {
                //访问失败
                //记录异常日志
                bo = false;
            }
            return bo;
        }

        /// <summary>
        /// 模拟访问页面，检测数据库可用。
        /// </summary>
        /// <param name="adrress"></param>
        /// <returns></returns>
        private static bool TestDataAdrress(string adrress)
        {
            bool bo = false;
            try
            {
                //实例化模拟客户端请求对象
                System.Net.WebRequest webRequest = System.Net.WebRequest.Create(adrress);
                //得到请求的响应对象
                System.Net.WebResponse webResponse = webRequest.GetResponse();

                if (webResponse.Headers.GetValues("result") != null)
                {
                    string[] strResults = webResponse.Headers.GetValues("result");
                    if (strResults.Length > 0)
                    {
                        if (strResults[0].ToLower().Equals("true"))
                        {
                            //数据库可用。
                            bo = true;
                        }
                        else
                        {
                            //数据库不可用
                            bo = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //访问失败
                //记录异常日志
                bo = false;
            }
            return bo;
        }

        /// <summary>
        /// 更新发送短信的次数
        /// </summary>
        /// <param name="item">某个站点的配置信息节点</param>
        /// <param name="date">日期（发送短信次数根据日期分组）</param>
        /// <returns>返回发送后的次数</returns>
        private static int UpdateTimesByWebsiteName(XmlNode item, string date)
        {
            int times = 0;

            if (doc == null)
            {
                throw new Exception("The File Websites.xml Exception!");
            }

            if (String.IsNullOrEmpty(date))
            {
                date = DateTime.Now.ToString("yyyy-MM-dd");
            }


            XmlNode node_SmsEmails = item.SelectSingleNode("SmsEmails");
            if (node_SmsEmails == null)
            {
                #region 判断是否包含SmsEmails节点，不包含则表示第一次发，创建SmsEmails和SmsEmail节点

                node_SmsEmails = doc.CreateElement("SmsEmails");
                XmlNode node_SmsEmails_SmsEmail = doc.CreateElement("SmsEmail");
                XmlAttribute attrTimes = doc.CreateAttribute("times");
                attrTimes.Value = "1";
                XmlAttribute attrDate = doc.CreateAttribute("date");
                attrDate.Value = date;
                node_SmsEmails_SmsEmail.Attributes.Append(attrTimes);
                node_SmsEmails_SmsEmail.Attributes.Append(attrDate);
                node_SmsEmails.AppendChild(node_SmsEmails_SmsEmail);
                item.AppendChild(node_SmsEmails);
                doc.Save(filePath);
                #endregion
            }
            else
            {
                XmlNodeList listSms = item.SelectNodes("SmsEmails//SmsEmail");
                for (int j = 0; j < listSms.Count; j++)
                {
                    if (String.Equals(listSms.Item(j).Attributes["date"].Value, date))
                    {
                        times = Convert.ToInt32(listSms.Item(j).Attributes["times"].Value);
                        if (times > -1 && times < Convert.ToInt32(item.Attributes["notificationMax"].Value))
                        {
                            listSms.Item(j).Attributes["times"].Value = (times + 1).ToString();
                        }
                        //停止内层循环
                        break;
                    }
                    else if (j == listSms.Count - 1)
                    {
                        //最后一次循环，如果未找到当天发送信息记录，则表示当天第一次发送。
                        #region 当天第一次发送信息，创建SmsEmail节点

                        XmlNode node_SmsEmails_SmsEmail = doc.CreateElement("SmsEmail");
                        XmlAttribute attrTimes = doc.CreateAttribute("times");
                        attrTimes.Value = "1";
                        XmlAttribute attrDate = doc.CreateAttribute("date");
                        attrDate.Value = date;
                        node_SmsEmails_SmsEmail.Attributes.Append(attrTimes);
                        node_SmsEmails_SmsEmail.Attributes.Append(attrDate);
                        node_SmsEmails.AppendChild(node_SmsEmails_SmsEmail);
                        doc.Save(filePath);
                        #endregion
                    }
                }
            }

            doc.Save(filePath);
            return times + 1;
        }

        #endregion
        */
        #endregion
    }
}
